import Vue from 'vue'
/** 搜索指定value的上级
 * organizations： 源对象
 * treeId： 要搜索的nodeId
 */
export const getParentNode = (organizations: Array<string>, treeId: string) => {
    // 保存每一个匹配的父节点id
    const arrRes: Array<string> = []
    const parentTree: Array<string> = []

    /** 保存每一个父节点 */
    const rev = (data: Array<string>, treeId2: string) => {
        for (let i = 0, length = data.length; i < length; i++) {
            const node: any = data[i]
            if (node.id === treeId2) {
                // 保存当前匹配节点的id号
                arrRes.unshift(treeId2)
                parentTree.unshift(node.value)
                // 放入匹配节点的父节点id
                rev(organizations, node.parent)
                // 跳出
                break
            } else {
                // 循环子节点
                if (node.children) {
                    rev(node.children, treeId2)
                }
            }
        }
    }
    rev(organizations, treeId)
    return parentTree
}

/** 返回当前节点的id
 * organizations: 原数组
 * treeName: 查找的nodeName
 */
export const getCurrentNodeId = (
    organizations: Array<string>,
    treeName: string
) => {
    let cloneTree = ''
    const getId = (data: Array<string>, treeName2: string) => {
        for (let i = 0, length = data.length; i < length; i++) {
            const tree: any = data[i]
            if (tree.value === treeName2) {
                // 返回匹配id
                cloneTree = tree.id
                break
            } else {
                // 循环子节点
                if (tree.children) {
                    getId(tree.children, treeName2)
                }
            }
        }
    }
    getId(organizations, treeName)

    return cloneTree
}

/** 返回要插入节点的正确id
 * organizations： 源对象
 * treeName： 要插入位置（父节点）名
 */
export const getDynamicNodeId = (
    organizations: Array<string>,
    treeName: string
) => {
    let childrenLength = 0
    const getId = (data: Array<string>, treeName2: string) => {
        for (let i = 0, length = data.length; i < length; i++) {
            const tree: any = data[i]
            // 处理匹配id的树
            if (tree.id === treeName2) {
                // tree.leaf = false
                Vue.set(tree, 'leaf', false)
                if (tree.children) {
                    childrenLength = tree.children.length
                }
                break
            } else {
                // 循环子节点
                if (tree.children) {
                    getId(tree.children, treeName2)
                }
            }
        }
    }
    getId(organizations, treeName)
    return childrenLength + 1
}

/** 格式化添加的组织树信息 */
export const normalizeTree = (tree: Array<string>, data: any) => {
    const parentNodeId = data.parentNodeId

    // 格式化插入树的信息
    const appendNodeInfo = {
        id: parentNodeId + '-' + getDynamicNodeId(tree, parentNodeId),
        parent: parentNodeId,
        label: data.companyName,
        value: data.companyName,
        children: null,
        leaf: true
    }
    return { parentNodeId, appendNodeInfo }
}

/** 插入组织树，
 * resource: 源对象
 * addNodeInfo：插入节点的信息：父节点id，node信息
 */
export const appendTreeNode = (
    resource: Array<string>,
    addNodeInfo: Array<string>
) => {
    // 格式化插入的组织树信息
    const { parentNodeId, appendNodeInfo } = normalizeTree(
        resource,
        addNodeInfo
    )

    const appendNode = (
        data: Array<string>,
        treeNode: object,
        parentId: string
    ) => {
        for (let i = 0, length = data.length; i < length; i++) {
            const tree: any = data[i]
            // 处理匹配id的树
            if (tree.id === parentId) {
                if (!tree.children) tree.children = []
                tree.children.push(treeNode)
                /** set响应式 */
                Vue.set(tree, 'children', tree.children)
                break
            } else {
                // 循环子节点
                if (tree.children) {
                    appendNode(tree.children, treeNode, parentId)
                }
            }
        }
    }
    appendNode(resource, appendNodeInfo, parentNodeId)
    return resource
}
